package util;

import java.util.*;

import org.joda.time.DateTime;
import org.joda.time.Days;

import bll.*;
import model.*;

public class HtmlUtil {

	public static String genHtml(boolean value) {
		return value ? "Y" : "N";
	}

	public static String genHtml(boolean isSet, boolean value) {

		// TODO :

		String html = "";
		if (isSet) {
			if (value) {
				html = "Yes";
			} else {
				html = "No";
			}
		}
		return html;
	}

	public static String genHtml(String text) {
		String html = text.replaceAll("\n", "<br />");
		return html;
	}

	public static <T extends IModel> String genModelSelect(List<T> data, String optionTemplate) {
		String html = genModelSelect(data, optionTemplate, Integer.MIN_VALUE);
		return html;
	}

	public static <T extends IModel> String genModelSelect(List<T> data, String optionTemplate, int selectedId) {
		return genModelSelect(data, optionTemplate, selectedId, false, false);
	}

	public static <T extends IModel> String genModelSelect(List<T> data, String optionTemplate, int selectedId, boolean isReadOnly) {
		String html = genModelSelect(data, optionTemplate, selectedId, isReadOnly, false);
		return html;
	}

	public static <T extends IModel> String genModelSelect(List<T> data, String optionTemplate, int selectedId, boolean isReadOnly, boolean showFirstItem) {

		String template = optionTemplate;
		if (template == null || template.isEmpty()) {
			template = "<option value=\":id\" :selected>:name</option>";
		}

		StringBuilder sb = new StringBuilder();

		if (!isReadOnly && showFirstItem) {
			String firstItemText = "--------------------";
			String selected = "";
			if (selectedId != Integer.MIN_VALUE || selectedId != Integer.MAX_VALUE) {
				selected = "selected=\"selected\"";
			}
			String html = template.replaceAll(":id", Integer.toString(Integer.MIN_VALUE)).replaceAll(":name", firstItemText).replaceAll(":selected", selected);
			sb.append(html);
			sb.append("\n");
		}

		for (int i = 0; i < data.size(); i++) {
			T model = data.get(i);

			if (isReadOnly) {
				if (model.getId() == selectedId) {
					String selected = "selected=\"selected\"";
					String id = Integer.toString(model.getId());
					String name = model.getName();

					String html = template.replaceAll(":id", id).replaceAll(":name", name != null ? name : "").replaceAll(":selected", selected);

					sb.append(html);
					sb.append("\n");
					break;
				}
			} else {
				String selected = "";
				if (selectedId == model.getId()) {
					selected = "selected=\"selected\"";
				}

				String id = Integer.toString(model.getId());
				String name = model.getName();

				String html = template.replaceAll(":id", id).replaceAll(":name", name != null ? name : "").replaceAll(":selected", selected);

				sb.append(html);
				sb.append("\n");
			}
		}

		return sb.toString();
	}

	public static String genEnumSelect(String enumName, String optionTemplate) {
		String html = genEnumSelect(enumName, optionTemplate, Integer.MIN_VALUE);
		return html;
	}

	public static String genEnumSelect(String enumName, String optionTemplate, int selectedId) {

		String template = optionTemplate;
		if (template == null || template.isEmpty()) {
			template = "<option value=\":id\" :selected>:name</option>";
		}

		StringBuilder sb = new StringBuilder();

		if (enumName.equalsIgnoreCase(EnumName.ProjectStatus)) {

			for (Enums.ProjectStatus status : Enums.ProjectStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.QuotationItemStatus)) {

			for (Enums.QuotationItemStatus status : Enums.QuotationItemStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.QuotationStatus)) {

			for (Enums.QuotationStatus status : Enums.QuotationStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.AgendaStatus)) {

			for (Enums.AgendaStatus status : Enums.AgendaStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.AgendaItemStatus)) {

			for (Enums.AgendaItemStatus status : Enums.AgendaItemStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.ChecklistItemStatus)) {

			for (Enums.ChecklistItemStatus status : Enums.ChecklistItemStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.ChecklistStatus)) {

			for (Enums.ChecklistStatus status : Enums.ChecklistStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.PhaseStatus)) {

			for (Enums.PhaseStatus status : Enums.PhaseStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		}	else if (enumName.equalsIgnoreCase(EnumName.Gender)) {

			for (Enums.Gender gender : Enums.Gender.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == gender.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(gender.getId())).replaceAll(":name", gender.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.PurchaseStatus)) {

			for (Enums.PurchaseStatus status : Enums.PurchaseStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else if (enumName.equalsIgnoreCase(EnumName.PurchaseItemStatus)) {

			for (Enums.PurchaseItemStatus status : Enums.PurchaseItemStatus.values()) {
				String selected = "";
				if (selectedId != Integer.MIN_VALUE && selectedId == status.getId()) {
					selected = "selected=\"selected\"";
				}

				String html = template.replaceAll(":id", Integer.toString(status.getId())).replaceAll(":name", status.getName()).replaceAll(":selected", selected);
				sb.append(html);
			}
		} else {

			// TODO : more enum here

		}

		return sb.toString();
	}

	public static String genCalendarMonthHeader(ProjectModel project) {

		StringBuilder sb = new StringBuilder();

		StyleInfo styleOption = project.getStyleInfo();
		List<MonthInfo> months = project.getMonths();

		for (MonthInfo month : months) {
			sb.append("<td ");
			if (styleOption.getCellMonthStyle() != null && !styleOption.getCellMonthStyle().isEmpty()) {
				sb.append(" class=\"" + styleOption.getCellMonthStyle() + "\"");
			}
			sb.append("colspan=\"" + month.getNoOfDay() + "\"");
			sb.append(">");

			sb.append(String.format("%s/%s", month.getFirstDay().getMonthOfYear(), month.getFirstDay().getYear()));

			sb.append("</td>");
		}

		return sb.toString();
	}

	public static String genCalendarDayHeader(ProjectModel project) {

		StringBuilder sb = new StringBuilder();

		StyleInfo styleInfo = project.getStyleInfo();
		List<MonthInfo> months = project.getMonths();

		for (MonthInfo month : months) {
			DateTime date = DateTimeUtil.cloneDateTime(month.getFirstDay());

			while (!date.isAfter(month.getLastDay().toInstant())) {
				sb.append("<td ");

				sb.append("class=\"");

				String classes = styleInfo.getCellDayStyle();
				classes = StringUtil.concat(classes, styleInfo.getWeekdayStyle(date), _classSep);
				if (DateTimeUtil.isToday(date)) {
					classes = StringUtil.concat(classes, styleInfo.getCellDayTodayStyle(), _classSep);
				}

				sb.append(classes);
				sb.append("\"");

				sb.append(">");
				sb.append(date.getDayOfMonth());
				sb.append("</td>");

				// move ahead
				date = date.plusDays(1);
			}
		}

		return sb.toString();
	}

	public static String genPhaseRow(ProjectModel project) {

		StringBuilder sb = new StringBuilder();

		StyleInfo styleInfo = project.getStyleInfo();
		List<MonthInfo> months = project.getMonths();

		for (MonthInfo month : months) {
			DateTime date = DateTimeUtil.cloneDateTime(month.getFirstDay());

			while (!date.isAfter(month.getLastDay().toInstant())) {
				sb.append("<td ");

				sb.append("class=\"");

				String classes = styleInfo.getCellPhaseStyle();
				if (DateTimeUtil.isToday(date)) {
					classes = StringUtil.concat(classes, styleInfo.getCellPhaseTodayStyle(), " ");
				}

				sb.append(classes);

				sb.append("\"");

				sb.append(">");
				// sb.append(date.getDayOfMonth());
				sb.append("</td>");

				// move ahead
				date = date.plusDays(1);
			}
		}

		return sb.toString();
	}

	public static String genHeaderRow(ProjectModel project) {

		StringBuilder sb = new StringBuilder();

		StyleInfo styleInfo = project.getStyleInfo();
		List<MonthInfo> months = project.getMonths();

		for (MonthInfo month : months) {
			DateTime date = DateTimeUtil.cloneDateTime(month.getFirstDay());

			while (!date.isAfter(month.getLastDay().toInstant())) {
				sb.append("<td ");

				sb.append("class=\"");

				String classes = styleInfo.getCellHeaderStyle();
				if (DateTimeUtil.isToday(date)) {
					classes = StringUtil.concat(classes, styleInfo.getCellDayTodayStyle(), _classSep);
				}

				sb.append(classes);

				sb.append("\"");

				sb.append(">");
				sb.append("</td>");

				// move ahead
				date = date.plusDays(1);
			}
		}

		return sb.toString();
	}

	private static final String _classSep = " ";

	public static String genTaskRow(ProjectModel project, ChecklistItemModel task) {

		StringBuilder sb = new StringBuilder();

		StyleInfo styleInfo = project.getStyleInfo();
		List<MonthInfo> months = project.getMonths();

		for (MonthInfo month : months) {
			DateTime date = DateTimeUtil.cloneDateTime(month.getFirstDay());

			while (!date.isAfter(month.getLastDay().toInstant())) {
				sb.append("<td ");

				sb.append("class=\"");

				// add default class for cell
				String classes = styleInfo.getCellStyle();

				// if class for task
				if (DateTimeUtil.inDayRange(date, new DateTime(task.getStartTime()), new DateTime(task.getEndTime()))) {
					classes = StringUtil.concat(classes, styleInfo.getCellTaskStyle(), _classSep);
				}

				// add class for today
				if (DateTimeUtil.isToday(date)) {
					classes = StringUtil.concat(classes, styleInfo.getCellTaskTodayStyle(), _classSep);
				}

				// TODO apply color of task

				sb.append(classes);

				sb.append("\"");

				sb.append(">");
				// sb.append(date.getDayOfMonth());
				sb.append("</td>");

				// move ahead
				date = date.plusDays(1);
			}
		}

		return sb.toString();
	}
}
